<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>获取用户OpenID</title>
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
    <meta content="black" name="apple-mobile-web-app-status-bar-style" />
    <meta content="telephone=yes" name="format-detection" />
    <link href="css/index.css" rel="stylesheet" type="text/css">
    <link href="css/OAuth.css" rel="stylesheet" type="text/css">
    <script src="js/jquery-1.8.3.min.js"></script>
    <script src="js/layer/layer.js"></script>
    <script src="js/json2.js"></script>
</head>
<body>
<div class="content">
<%
	String code = request.getParameter("code");
	String state = request.getParameter("state");
%>
    <div class="img"><img src="images/weixin.png" id="img"> </div>
    <div class="content-0">
        <div class="content-1">OpenId</div>
        <div class="content-2"><input type="text" id="OpenId"></div>
        <div class="content-1">昵称</div>
        <div class="content-2"><input type="text" id="nick"></div>
        <div class="content-1">性别</div>
        <div class="content-2"><input type="text" id="sex"></div>
        <div class="content-1">国家</div>
        <div class="content-2"><input type="text" id="country"></div>
        <div class="content-1">省</div>
        <div class="content-2"><input type="text" id="province"></div>
        <div class="content-1">城市</div>
        <div class="content-2"><input type="text" id="city"></div>
        <div class="content-1">特权</div>
        <div class="content-2"><input type="text" id="privilege"></div>
        <div>code:<%=code %></div>
    </div>
</div>
</body>
</html>
<script type="text/javascript">

$(function(){
	var index =layer.load(2,{content:"loading..."});
	/*根据获取的code值 ，获取 refresh_token*/
	$.ajax({
		url:"http://www.accecard.com/nsy-web/api/card/centerService",
		dataType:"json",
		data:{
			serviceCode:"0000",
			domain:"https://api.weixin.qq.com/sns/oauth2/access_token",
			appid:"wx167b18378241425a",
			secret:"4bba534321b209d2d3dd8a2e35b630b1",
			code:"<%=code %>",
			grant_type:"authorization_code"
		},
		complete:function(xhr,status){
			var costr = xhr.responseText;
        	var co = JSON.parse(costr);
        	var appid = co.appid;
        	var grant_type = co.grant_type;
        	var refresh_token = co.refresh_token;
        	/*根据获取的refresh_token值 ，获取 openid和access_token*/
        	$.ajax({
        		url:"http://www.accecard.com/nsy-web/api/card/centerService",
        		dataType:"json",
        		data:{
        			serviceCode:"0000",
        			domain:"https://api.weixin.qq.com/sns/oauth2/refresh_token",
        			appid:"wx167b18378241425a",
        			grant_type:"refresh_token",
        			refresh_token:refresh_token
        		},
        		complete:function(xhr,status){

        			var opstr = xhr.responseText;
                	var op = JSON.parse(opstr);
                	var access_token = op.access_token;
                	var openid = op.openid;
                	/*根据获取 openid和access_token，获取用户基本信息，包括OpenId*/
                	$.ajax({
                        url:"http://www.accecard.com/nsy-web/api/card/centerService",
                        dataType: "json",
                        data: {
                        	serviceCode: "0000",
                        	domain: "https://api.weixin.qq.com/sns/userinfo",
                        	access_token: access_token,
                        	openid: openid
                        },
                        complete: function(xhr, status) {

                        	var userstr = xhr.responseText;
                        	var user = JSON.parse(userstr);
                        	layer.closeAll();
                        	$("#nick").val(user.nickname);
                        	$("#OpenId").val(user.openid);
                        	if(user.sex == 1) {
                        		$("#sex").val('男');
                        	}
                        	else if(user.sex == 2){
                        		$("#sex").val("女")
                        	}
                        	else if(user.sex == 0){
                        		$("#sex").val('未知');
                        	}
                        	$("#country").val(user.country);
                    
                        	$("#city").val(user.city);
                        	$("#province").val(user.province);
                        	$("#privilege").val(user.privilege);
                        	if(user.privilege==""){
                        		$("#privilege").val("无");
                        	}
                        	$("#img").attr("src",user.headimgurl)
                        }
                    })
        		}
        	})
		}
	})
	
});

</script>